﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DropDownList1.Items.Add("福建");
                DropDownList1.Items.Add("广东");
                DropDownList1.Items.Add("山东");
                DropDownList.Items.Add("漳州");
                DropDownList.Items.Add("泉州");
                DropDownList.Items.Add("福州");
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string p = DropDownList1.SelectedValue;
           // DropDownList1.Items.Clear();错误代码
            DropDownList.Items.Clear();
            if (p == "福建")
            {
                DropDownList.Items.Add("漳州");
                DropDownList.Items.Add("泉州");
                DropDownList.Items.Add("福州");
            }
            else if (p == "广东")
            {
                DropDownList.Items.Add("顺德");
                DropDownList.Items.Add("广州");
                DropDownList.Items.Add("汕头");
            }
            else if (p == "山东")
            {
                DropDownList.Items.Add("济南");
                DropDownList.Items.Add("临沂");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //判断是否上传了文件
            if (FileUpload1.HasFile)
            {
                //接收上传的文件名
                string fileName = FileUpload1.FileName;
                //判断文件名 是否以.jpg/.png/.jpeg结尾 如果是就代表文件是图片
                if (fileName.EndsWith(".jpg") || fileName.EndsWith(".png") || fileName.EndsWith(".jpeg"))
                {
                    //获取到文件的真实路径
                    string url = Server.MapPath("/img/" + fileName);
                    //将文件保存到制定目录下
                    FileUpload1.SaveAs(url);
                    //设置图片控件的文件路径
                    Image1.ImageUrl = "/img/" + fileName;
                    //设置图片控件为显式的（显示在页面上）
                    Image1.Visible = true;
                }
            }
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string Hobby = null;

            //遍历多选框选项的集合
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)//判断遍历到的这个元素是否被选中
                {
                    Hobby += CheckBoxList1.Items[i]+" ";//如果选中 就拼接到上面的变量里面 
                }
            }

            Label1.Text = $"用户名：{TextBox2.Text},性别：{RadioButtonList1.SelectedItem},爱好：{Hobby},{DropDownList1.SelectedValue},{DropDownList.SelectedValue}";
            //,{FileUpload1}这个是文件上传 不需要输出
        }
    }
}